package com.henghai.zhenxuan.ui.view;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;

import com.henghai.zhenxuan.data.bean.AddressBean;
import com.blankj.utilcode.util.ToastUtils;
import com.henghai.zhenxuan.databinding.DialogAddNewAddressBinding;

public class AddEditingDialogFragment extends AppCompatDialogFragment {
    private static String KEY_ADDRESS_PARAMS = "key_address";
    private AddressBean mAddress;
    private DialogAddNewAddressBinding binding;
    private Window window;


    public static AddEditingDialogFragment newInstance(@Nullable AddressBean address) {
        Bundle args = new Bundle();
        args.putSerializable(KEY_ADDRESS_PARAMS, address);
        AddEditingDialogFragment fragment = new AddEditingDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private OnItemBtClickListener mItemBtClickListener;

    public AddEditingDialogFragment setOnItemBtClickListener(OnItemBtClickListener l) {
        this.mItemBtClickListener = l;
        return this;
    }

    public interface OnItemBtClickListener {

        void onEditClick();

        void onSaveClick(AddressBean addressBean);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        try {
//            binding = DialogAddNewAddressBinding.inflate(inflater);
//            window = getDialog().getWindow();
//            View contentView = binding.getRoot();
//            window.setBackgroundDrawable(ResourceUtils.getDrawable(R.color.color_transparent));
//            return contentView;
//        } catch (Exception exception) {
        binding = DialogAddNewAddressBinding.inflate(inflater);
        return binding.getRoot();
//        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAddress = (AddressBean) getArguments().getSerializable(KEY_ADDRESS_PARAMS);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (mAddress != null) {
            binding.userName.getEditText().setText(mAddress.getConsigneeName());
            binding.userPhone.getEditText().setText(mAddress.getConsigneePhone());
            binding.addressDetail.getEditText().setText(mAddress.getConsigneeAddress());
        }

        binding.addressDetail.getTitleView().setGravity(Gravity.TOP);
        binding.addressDetail.getTitleView().setPadding(0, 15, 0, 0);
        binding.addressDetail.getEditText().setGravity(Gravity.TOP);
        binding.addressDetail.getEditText().setSingleLine(false);
        binding.addressDetail.getEditText().setPadding(0, 15, 0, 0);

        binding.actionCancelAddress.setOnClickListener(v -> dismiss());
        binding.actionSaveAddress.setOnClickListener(v -> {
            if (binding.userName.getEditText().getText().toString().trim().isEmpty()) {
                ToastUtils.showShort("姓名不能为空");
                return;
            }
            if (binding.userPhone.getEditText().getText().toString().trim().isEmpty()) {
                ToastUtils.showShort("手机号不能为空");
                return;
            }

            if (binding.userPhone.getEditText().getText().toString().trim().length() != 11) {
                ToastUtils.showShort("请填写正确手机号");
                return;
            }

            if (binding.addressDetail.getEditText().getText().toString().trim().isEmpty()) {
                ToastUtils.showShort("地址不能为空");
                return;
            }

            if (mAddress == null) {
//                DataRepository.getInstance().addConsigneeInfo(getViewLifecycleOwner(), new AddConsigneeInfoApi(new
//                        ConsigneeRepositoryBean(binding.userName.getEditText().getText().toString(),
//                        binding.userPhone.getEditText().getText().toString()
//                        , binding.addressDetail.getEditText().getText().toString(),
//                        "0"
//                )), dataResult -> {
//                    if (!dataResult.getResponseStatus().isSuccess()) {
//                        ToastUtils.showShort(dataResult.getResponseStatus().getResponseCode());
//                        return;
//                    }
//                    dismiss();
//                    if (mItemBtClickListener != null) {
//                        mItemBtClickListener.onSaveClick(
//                                new AddressBean(binding.userName.getEditText().getText().toString(), binding.userPhone.getEditText().getText().toString(), binding.addressDetail.getEditText().getText().toString()
//                                ));
//                    }
//                    TipDialog.show("保存成功", WaitDialog.TYPE.SUCCESS);
//                });
            } else {
                if (mAddress.getConsigneeName().equals(binding.userName.getEditText().getText().toString()) &&
                        mAddress.getConsigneeAddress().equals(binding.addressDetail.getEditText().getText().toString()) &&
                        mAddress.getConsigneePhone().equals(binding.userPhone.getEditText().getText().toString())) {
                    if (mItemBtClickListener != null) {
                        mItemBtClickListener.onSaveClick(
                                new AddressBean(
                                        binding.userName.getEditText().getText().toString(),
                                        binding.userPhone.getEditText().getText().toString(),
                                        binding.addressDetail.getEditText().getText().toString()
                                ));
                    }
                    dismiss();
                    return;
                }
//                DataRepository.getInstance().updateConsigneeInfo(getViewLifecycleOwner(), new UpdateConsigneeInfoApi(new
//                        ConsigneeRepositoryBean(mAddress.getId(), binding.userName.getEditText().getText().toString(),
//                        binding.userPhone.getEditText().getText().toString()
//                        , binding.addressDetail.getEditText().getText().toString(),
//                        mAddress.getIsDefault()
//                )), dataResult -> {
//                    if (!dataResult.getResponseStatus().isSuccess()) {
//                        ToastUtils.showShort(dataResult.getResponseStatus().getResponseCode());
//                        return;
//                    }
//                    dismiss();
//                    TipDialog.show("修改成功", WaitDialog.TYPE.SUCCESS);
//                    if (mItemBtClickListener != null) {
//                        mItemBtClickListener.onEditClick();
//                    }
//                });
            }


        });
    }
}
